<?php
	require 'config.php';
	require 'init.php';
	/**/
	function cleanStr($strArray){
		if(is_array($strArray)){
			foreach ($strArray as &$val) {
				$val = str_replace("'",'',$val);
				$val = trim($val);
			}
		} else {
			$strArray = trim($strArray);
			$strArray = str_replace("'",'',$strArray);
		}
		
		return $strArray;
	}
	/**/
	function converttoArray($str){
		$matches = array();
		foreach ($str as $s) {
			$match = explode(',',$s);
			$matches[cleanStr($match[2])] = cleanStr($match);
		}
		
		return $matches;
	}
	/**/
	function getInfoMatch($infoMatchString){
		$infoMatchString = str_replace(" ",'',$infoMatchString);
		$infoMatchString = str_replace("'",'',$infoMatchString);
		$infoMatchString = str_replace(");",'',$infoMatchString);
		$infoMatchString = str_replace("bettingESInfo(",'',$infoMatchString);
		return explode(',',$infoMatchString);
	}
	// init category matche
	$cates = CTable('category_match')->select_all();
	$cateMatch = array();
	foreach ($cates as $cate) {
		$cateMatch[$cate['name']] = $cate['id'];
	}
	// init match
	$preMactches = CTable('match')->select_all(array('match_end'=>0));
	$matchAdded = array();
	foreach ($preMactches as $pm) {
		$matchAdded[$pm['code']] = $pm['id'];
	}
	//get data win29
	$win29 = file_get_contents('http://win29.com/betting.es.php');
	preg_match_all("/addESC\((.*)\);/", $win29, $out);
	$matches = converttoArray($out[1]);	
	foreach ($matches as $match) {
		// add catgegory
		if(!isset($cateMatch[$match[1]])){
			$id = CTable('category_match')->insert(array(
				'name' => $match[1]
			));
			$cateMatch[$match[1]] =$id;
		}
		
		//add matches
		if(!isset($matchAdded[$match[2]])){
//		if(true){
			$id = CTable('match')->insert(array(
				'cate_id' => $cateMatch[$match[1]],
				'code' => $match[2],
				'team1' => $match[4],
				'team2' => $match[5],
				'match_start' => $match[3],
				'public' => 0
			));
			
			$matchAdded[trim($match[2])] =$id;
			//echo 'http://www.win29.com/betting.es.php?page=info&matchID='.$match[2].'<br>';
			// add rate
			$infoMatch = file_get_contents('http://www.win29.com/betting.es.php?page=info&matchID='.$match[2]);
			$infoMatch = getInfoMatch($infoMatch);

			$idrate = CTable('match_rate')->insert(array(
				'match_id' => $id,
				'ft_home' => $infoMatch[5],
				'ft_draw' => $infoMatch[6],
				'ft_away' => $infoMatch[7],
				'dc_hod' => $infoMatch[8],
				'dc_aod' => $infoMatch[9],
				'dc_hoa' => $infoMatch[10],
				'ah_home' => $infoMatch[12],
				'ah_away' => $infoMatch[13],
				'ah_rate_goal' => $infoMatch[11],
				'ahs_home' => $infoMatch[21],
				'ahs_away' => $infoMatch[22],
				'ahs_rate_goal' => $infoMatch[20],
				'ou_over' => $infoMatch[19],
				'ou_rate_goal' => $infoMatch[17],
				'ou_under' => $infoMatch[18],
				'ts_even' => 1.95,
				'ts_odd' => 1.95
			));
		}else{
			$infoMatch = file_get_contents('http://www.win29.com/betting.es.php?page=info&matchID='.$match[2]);
			$infoMatch = getInfoMatch($infoMatch);
			$id = $matchAdded[$match[2]];
			$idrate = CTable('match_rate')->insert(array(
				'match_id' => $id,
				'ft_home' => $infoMatch[5],
				'ft_draw' => $infoMatch[6],
				'ft_away' => $infoMatch[7],
				'dc_hod' => $infoMatch[8],
				'dc_aod' => $infoMatch[9],
				'dc_hoa' => $infoMatch[10],
				'ah_home' => $infoMatch[12],
				'ah_away' => $infoMatch[13],
				'ah_rate_goal' => $infoMatch[11],
				'ahs_home' => $infoMatch[21],
				'ahs_away' => $infoMatch[22],
				'ahs_rate_goal' => $infoMatch[20],
				'ou_over' => $infoMatch[19],
				'ou_rate_goal' => $infoMatch[17],
				'ou_under' => $infoMatch[18],
				'ts_even' => 1.95,
				'ts_odd' => 1.95
			));
		}		
	}
	echo "<pre>";
    print_r($matchAdded);
